<template>
	<!-- 商品卡片 -->
	<view class="goodCard">
		<!-- 商品项 -->
		<view class="good">
			<!-- 图片 -->
			<view class="good-image">
				<image :src="goodImage" mode="widthFix|aspectFill" class="good-image__image"></image>
			</view>
			<!-- 信息 -->
			<view class="good-info">
				<!-- 名称 -->
				<view class="good-title">{{good.name}}</view>
				<!-- 描述 与 步进器 -->
				<view class="desc">
					<!-- 描述 -->
					<view class="good-desc" :class="showNumberBox?'textOverflow':'multiTextOverFlow'">
						<slot name="desc">默认</slot>
					</view>
					<!-- 步进器 -->
					<view class="good-numberBox" v-if="showNumberBox">
						<u-number-box v-model="value" min="0" :disableMinus="value<=0" @change="numChanged">
							<!-- 减按钮 -->
							<view slot="minus">
								<view class="num-subBtn"><i class="num-subBtn__text">-</i></view>
							</view>
							<!-- 数值 -->
							<text slot="input" style="width: 60rpx;text-align: center;"
								class="num-input">{{value}}</text>
							<!-- 加按钮 -->
							<view slot="plus">
								<view class="num-addBtn"><i class="num-addBtn__text">+</i></view>
							</view>
						</u-number-box>
						<!-- <slot name="numberBox" :data="good"></slot> -->
					</view>
				</view>
				<!-- 价格 与 按钮 -->
				<view class="price">
					<text class="price-text">￥{{good.price}}</text>
					<view class="price-button" v-if="showSkuBtn">
						<u-button text="选规格" shape="circle" :customStyle="priceButton"
							@click="onClickSku(good)"></u-button>
					</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		options: {
			// 允许样式穿透
			styleIsolation: 'shared'
		},
		name: "goodCard",
		props: {
			/* 
				商品信息
				good:{
					name: '',
					price: '',
					image: '',  // 或者s_goods_photos[0].path
				}
			 */
			good: {
				type: Object,
				default: () => {},
			},
			// 是否展示规格按钮
			showSkuBtn: {
				type: Boolean,
				default: true
			},
			// 是否展示步进器
			showNumberBox: {
				type: Boolean,
				default: false
			}
		},
		data() {
			return {
				// 样式
				priceButton: {
					height: '48rpx',
					width: '100rpx',
					backgroundColor: '#ff516b'
				},
				// 步进器数值
				value: 1,
			};
		},
		computed: {
			// 商品图片
			goodImage() {
				return this.good?.s_goods_photos?.[0]?.path || this.good?.image || '';
			},
		},
		watch: {
			// 监听商品数量变化
			good: {
				handler() {
					this.value = this.good.num;
				},
				deep: true,
				immediate: true
			}
		},
		methods: {
			// 点击规格按钮
			onClickSku(good) {
				// 通知父组件
				this.$emit('clickSku', good);
			},
			// 步进器数值变化
			numChanged(value) {
				this.$emit('numChanged', value, this.good.id);
			},
		}
	}
</script>

<style lang="scss" scoped>
	// 商品项
	.good {
		display: flex;
		padding: 0 10rpx 10rpx;
		margin-bottom: 20rpx;

		// 图片
		.good-image {
			flex-shrink: 0;
			height: 150rpx;
			width: 150rpx;
			padding-top: 20rpx;
			box-sizing: border-box;
			overflow: hidden;

			.good-image__image {
				height: 100%;
				width: 100%;
			}
		}

		// 信息
		.good-info {
			flex: 1;
			display: flex;
			flex-direction: column;
			justify-content: space-between;
			margin-left: 14px;

			// 名称
			.good-title {
				font-size: 28rpx;
				line-height: 1.5em;
			}

			// 描述 与 步进器
			.desc {
				display: flex;
				align-items: center;
				justify-content: space-between;
				width: 100%;

				// 描述
				.good-desc {
					flex: 1;
					font-size: 22rpx;
					color: #999;
					line-height: 1.4em;
					margin-top: 2rpx;

					&.textOverflow {
						@include multiTextOverFlow(1);
					}

					&.multiTextOverFlow {
						@include multiTextOverFlow(2);
					}
				}

				// 步进器
				.good-numberBox {
					flex-shrink: 0;
					margin-left: 20rpx;

					// 减按钮
					.num-subBtn {
						height: 36rpx;
						width: 36rpx;
						border-radius: 50%;
						border: 2px solid $theme-color;
						box-sizing: border-box;

						.num-subBtn__text {
							color: $theme-color;
							font-size: 40rpx;
							font-weight: 700;
							text-align: center;
							line-height: 18rpx;
						}
					}


					// 加按钮
					.num-addBtn {
						height: 36rpx;
						width: 36rpx;
						border-radius: 50%;
						border: 2px solid $theme-color;
						box-sizing: border-box;
						background-color: $theme-color;

						.num-addBtn__text {
							color: #fff;
							font-size: 30rpx;
							font-weight: 700;
							text-align: center;
							line-height: 22rpx;
						}
					}
				}
			}



			// 价格与按钮
			.price {
				display: flex;
				justify-content: space-between;
				margin-top: 8rpx;

				.price-text {
					font-size: 28rpx;
					color: $theme-color;
				}

				.price-button {
					::v-deep .u-button__text {
						color: #fff;
						font-size: 20rpx !important;
					}
				}
			}
		}
	}
</style>